package environment;

import java.util.ArrayList;

import logic.Indy;

public class Environment {

	public static enum Destination {
		LEFT, RIGHT, UP, DOWN
	}

	private Cell[][] grid;
	private Indy indy;

	private Cell chest;
	private Cell gem1;
	private Cell gem2;
	private Cell gem3;
	private Cell gem4;

	private boolean gem1Collected = false;
	private boolean gem2Collected = false;
	private boolean gem3Collected = false;
	private boolean gem4Collected = false;
	
	public boolean isGem1Collected() {
		return gem1Collected;
	}

	public void setGem1Collected(boolean gem1Collected) {
		this.gem1Collected = gem1Collected;
	}

	public boolean isGem2Collected() {
		return gem2Collected;
	}

	public void setGem2Collected(boolean gem2Collected) {
		this.gem2Collected = gem2Collected;
	}

	public boolean isGem3Collected() {
		return gem3Collected;
	}

	public void setGem3Collected(boolean gem3Collected) {
		this.gem3Collected = gem3Collected;
	}

	public boolean isGem4Collected() {
		return gem4Collected;
	}

	public void setGem4Collected(boolean gem4Collected) {
		this.gem4Collected = gem4Collected;
	}
	
	private Cell goalCell;

	public Cell getGoalCell() {
		return goalCell;
	}

	public void setGoalCell(Cell goalCell) {
		this.goalCell = goalCell;
	}

	public Environment(Cell[][] grid, Indy indy, Cell chest, Cell gem1,
			Cell gem2, Cell gem3, Cell gem4) {
		super();
		this.indy = indy;
		this.grid = grid;
		this.chest = chest;
		this.gem1 = gem1;
		this.gem2 = gem2;
		this.gem3 = gem3;
		this.gem4 = gem4;
	}
	

	public Cell[][] getGrid() {
		return grid;
	}

	public void setGrid(Cell[][] grid) {
		this.grid = grid;
	}

	public Indy getIndy() {
		return indy;
	}

	public void setIndy(Indy indy) {
		this.indy = indy;
	}

	public void moveIndy(int x, int y) {
		this.indy.move(x, y);
	}

	public Cell getChest() {
		return chest;
	}

	public void setChest(Cell chest) {
		this.chest = chest;
	}

	public Cell getGem1() {
		return gem1;
	}

	public void setGem1(Cell gem1) {
		this.gem1 = gem1;
	}

	public Cell getGem2() {
		return gem2;
	}

	public void setGem2(Cell gem2) {
		this.gem2 = gem2;
	}

	public Cell getGem3() {
		return gem3;
	}

	public void setGem3(Cell gem3) {
		this.gem3 = gem3;
	}

	public Cell getGem4() {
		return gem4;
	}

	public void setGem4(Cell gem4) {
		this.gem4 = gem4;
	}

	@Override
	public String toString() {
		return "Environment [indy=" + indy + ", chest=" + chest + ", gem1="
				+ gem1 + ", gem2=" + gem2 + ", gem3=" + gem3 + ", gem4=" + gem4
				+ ", goalCell=" + goalCell + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (obj != null && obj instanceof Environment) {
			return ((Environment) obj).indy.equals(this.indy)
					&& ((Environment) obj).goalCell.equals(this.goalCell);
		} else {
			return false;
		}
	}

	public void setGoalAsNearestGem() {
		if(goalCell!=null){
			if(goalCell.equals(gem1)){
				gem1Collected = true;
			} else if(goalCell.equals(gem2)){
				gem2Collected = true;
			} else if(goalCell.equals(gem3)){
				gem3Collected = true;
			} else if(goalCell.equals(gem4)){
				gem4Collected = true;
			}
		}
		ArrayList<Cell> gems = new ArrayList<Cell>();

		if (!gem1Collected) {
			gems.add(gem1);
		}
		if (!gem2Collected) {
			gems.add(gem2);
		}
		if (!gem3Collected) {
			gems.add(gem3);
		}
		if (!gem4Collected) {
			gems.add(gem4);
		}
		if (!gems.isEmpty()) {
			Cell actualGoal = (Cell)gems.get(0).clone();
			for (int i = 1; i < gems.size(); i++) {
				if (Math.abs(gems.get(i).x - indy.getLocation().x)
						+ Math.abs(gems.get(i).y - indy.getLocation().y) < Math
						.abs(actualGoal.x - indy.getLocation().x)
						+ Math.abs(actualGoal.y - indy.getLocation().y)) {
					actualGoal = gems.get(i);
				}
			}
			
			goalCell = (Cell) actualGoal.clone();
		} else {
			goalCell = (Cell) chest.clone();
		}

	}

	public Environment clone() {
		Environment newEnvironment = new Environment(grid, indy.clone(), chest,
				gem1, gem2, gem3, gem4);
		newEnvironment.setGoalCell((Cell)goalCell.clone());
		newEnvironment.setGem1Collected(gem1Collected);
		newEnvironment.setGem2Collected(gem2Collected);
		newEnvironment.setGem3Collected(gem3Collected);
		newEnvironment.setGem4Collected(gem4Collected);
		return newEnvironment;
	}
}
